package bridge;

import java.io.IOException;

import com.sun.org.apache.regexp.internal.RE;
import com.sun.org.apache.regexp.internal.RESyntaxException;

/**
 * @author bajo
 */

public class TableGenerator2ndRound extends TableGenerator {
	public final static String RESULTS_2ND_FILE_NAME = 
	    "C://eclipse//workspace//bridge2//WebContent//wyniki_wiosna2011.txt";
	
    public TableGenerator2ndRound() {
    }

	public static void main(String[] args) {
		TableGenerator2ndRound tableGenerator = new TableGenerator2ndRound();
		tableGenerator.openFiles(RESULTS_2ND_FILE_NAME);
		System.out.println("jestem!f!!");			
		tableGenerator.getTeamResults();
		tableGenerator.generateTable();
		tableGenerator.printTable();		
		tableGenerator.closeFiles();
	
	}
	
	/**
	 * 
	 */
	public void getTeamResults() {
		String line;	
		System.out.println("jestem!1!!");				
		try {
			while ((line = input.readLine()) != null) {
				System.out.println("jestem!!!"+line);						
				if (teamLineOK().match(line)) {
					String[] team = lineSpliter().split(line);
					Team2ndRound newTeam = new Team2ndRound(team[0], team[1]);
					int newTeamResults[] = new int[TableGenerator.NR_OF_TEAMS_IN_LEAGUE];
					newTeam.setResult1stRound(Integer.parseInt(team[2].trim()));
					for (int i = 0; i < newTeamResults.length; i++) {
						newTeamResults[i] = Integer.parseInt(team[i+3].trim());
					}
					newTeam.setResults(newTeamResults);
					System.out.println("jestem!2!!");	
					teams.add(newTeam);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static RE teamLineOK() {

			RE format = null;

			try {
				// there are 13 fields that can be passed into this expression
				// all mathed string has to be all line long (^ ... $)
				// one token is everything except ";" [^;]* plus ;
				format = new RE("^[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*;[^;]*$");
			} catch (RESyntaxException rese) {
				rese.printStackTrace();
			}        
        
			return format;
		}
}
